package com.ljx.SlidingWindow;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 李加喜
 * @Package com.ljx.SlidingWindow
 * @date 2021-11-10 11:42
 */

/**
 * 没有重复最长子串
 */
public class MaxLongString {
    public static void main(String[] args) {
        MaxLongString maxLongString = new MaxLongString();
        System.out.println(maxLongString.lengthOfLongestSubstring("pwwkew"));
    }
    int lengthOfLongestSubstring(String s) {
        Map<Character, Integer> window = new HashMap<>(16);
        char[] ss = s.toCharArray();
        int left=0,right=0;
        int valie=0;
        while (right<ss.length){
            char c=ss[right];
            right++;
            window.put(c,window.getOrDefault(c,0)+1);
            while (window.get(c)>1){
                char d= ss[left];
                left++;
                window.put(d,window.getOrDefault(d,0)-1);
            }
            valie=Math.max(valie,right-left);
        }
        return valie;
    }
}
